package net;

import java.io.*;
import java.net.InetAddress;
import java.net.Socket;

/**
 * TCP网络编程-客户端
 * (字符流)
 */
public class SocketTCP03Client {

    public static void main(String[] args) throws Exception {
        Socket socket = new Socket(InetAddress.getLocalHost(), 9999);
        System.out.println(socket.getClass());

        OutputStream outputStream = socket.getOutputStream();
        BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));
        InputStream inputStream = socket.getInputStream();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));


        bufferedWriter.write("hello, server 字符流");
        bufferedWriter.newLine();//插入一个换行符，表示写入的内容结束，要求对方使用readLine()读取内容
        //如果使用字符流，需要手动刷新写入
        bufferedWriter.flush();
//        socket.shutdownOutput();//设置写入结束标记

        String str = bufferedReader.readLine();
        System.out.println(str);

        if (str.equals("hello, client 字符流")) {
            System.out.println("接收到服务器响应:" + str);
            System.out.println("客户端退出...");
            outputStream.close();
            inputStream.close();
            socket.close();
        }
    }
}
